package com.snatch.snatchnd.core;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.snatch.common.vo.HeartbeatInput;
import com.snatch.common.vo.SnatchState;
import com.snatch.common.vo.WebConfig;
import com.snatch.snatchnd.service.AccountService;
import com.snatch.snatchnd.service.TaskService;
import com.snatch.snatchnd.service.impl.NodeIntegration;
import com.snatch.snatchnd.system.AccountData;
import com.snatch.snatchnd.system.SystemData;


@Component
public class Heartbeat extends Thread {
    private Logger logger = Logger.getLogger(Heartbeat.class);
    NodeIntegration nodeIntegration;
    AccountService accountManageService;
    TaskService taskService;
    Long intervalTime = 30L;

    @Override
    public void run() {
        while (true) {
            try {
                // 判断当前节点服务器是否有抓取线程存在
                // if (SystemData.snatchItemStatus.size() <= 0 && !(SystemData.getHasError())) {// 当抓取节点没有启动任何一个抓取项且注册节点时,是否存在错误的标识为false
                // if (SystemData.snatchItemStatus.size() <= 0) {// 当抓取节点没有启动任何一个抓取项时
                // logger.info(SystemData.nodeConfigs.getOnlyCode() + "节点无抓取线程存在!");
                long tiem = System.currentTimeMillis();
                HeartbeatInput heartbeatInput = new HeartbeatInput();
                heartbeatInput.setOnlyCode(SystemData.nodeWebs.getOnlyCode()); // 节点唯一标识码

                for (WebConfig sebConfig : SystemData.nodeWebs.getWebConfigs()) {
                    SnatchState snatchState = new SnatchState();
                    snatchState.setWebCode(sebConfig.getWebCode());

                    if (accountManageService.webValid(sebConfig.getWebCode())) {
                        snatchState.setOpen(true);
                        snatchState.setLoginAccunts(AccountData.validAccount(
                                sebConfig.getWebCode()));
                    }
                    snatchState.setSnatchItems(taskService.runSnatchTask(
                            sebConfig.getWebCode()));
                    String sytemtype = SystemData.systemType.get(sebConfig.getWebCode());
                    if(null != sytemtype){
                    	snatchState.setSystemCode(sytemtype);
                    }
                    heartbeatInput.getSnatchStates().add(snatchState);
                }

                nodeIntegration.heartbeat(heartbeatInput); // 需要每隔一段时间发送心跳包到管理中心
                logger.info("心跳时间："+(System.currentTimeMillis()-tiem));
                Thread.sleep(intervalTime * 1000);
            } catch (Exception e) {
                logger.info(e.getMessage(), e);
            }
        }
    }

    public NodeIntegration getNodeIntegration() {
        return nodeIntegration;
    }

    public void setNodeIntegration(NodeIntegration nodeIntegration) {
        this.nodeIntegration = nodeIntegration;
    }

    public Long getIntervalTime() {
        return intervalTime;
    }

    public AccountService getAccountManageService() {
        return accountManageService;
    }

    public void setAccountManageService(
        AccountService accountManageService) {
        this.accountManageService = accountManageService;
    }

    public TaskService getTaskService() {
        return taskService;
    }

    public void setTaskService(TaskService taskService) {
        this.taskService = taskService;
    }

    public void setIntervalTime(Long intervalTime) {
        this.intervalTime = intervalTime;
    }
}
